Skip to content

Conversation

@lesquoyb
Copy link
Contributor

first draft, not perfect yet

@AlexisDrogoul could you have a look ? I have a problem with this code: because I register a listener every time an agent does a connect, the cleanup function is called many times in example with multiple connections/multiple agents. It is working properly but I don't find it satisfying, and I don't know how to properly only register one listener per population (and per experiment I guess).

first draft, not perfect yet
@AlexisDrogoul
Copy link
Member

As the skill is attached to a species -- and hence to a population, I would try to only attach the listener once when the skill is created... I will try to see what can be done easily in this respect.

@lesquoyb lesquoyb requested a review from Copilot June 9, 2025 08:05
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR improves cleanup of network connections by ensuring streams and sockets are closed in ClientService and by auto-disconnecting agents through a new population listener in NetworkSkill.

  • Adjust stream/socket closing order in stopService
  • Introduce NetworkListener to disconnect agents when removed
  • Enhance disconnect to remove closed connectors and update agent state

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
gama.extension.network/src/gama/extension/network/tcp/ClientService.java Moved receiver.close() call after socket closure in stopService
gama.extension.network/src/gama/extension/network/skills/NetworkSkill.java Added NetworkListener, registered it per connection, and updated disconnect logic to prune connectors and NET_AGENT_SERVER list
Comments suppressed due to low confidence (2)

gama.extension.network/src/gama/extension/network/skills/NetworkSkill.java:374

  • [nitpick] The variable name connector_to_remove uses snake_case, which is inconsistent with Java's camelCase convention; rename it to connectorsToRemove or similar.
var connector_to_remove = new ArrayList<String>();

gama.extension.network/src/gama/extension/network/skills/NetworkSkill.java:366

  • The updated disconnect logic, including connector cleanup and attribute updates, isn't covered by existing tests; consider adding unit tests to validate this behavior.
public Boolean disconnect(final IScope scope) {

Comment on lines 112 to +115
if (socket != null) {
socket.close();
}
if (receiver != null) { receiver.close(); }
Copy link

Copilot AI Jun 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Closing the socket before closing the receiver stream can lead to resource leaks or exceptions; consider closing the receiver (input stream) before the socket.

Suggested change
if (socket != null) {
socket.close();
}
if (receiver != null) { receiver.close(); }
if (receiver != null) { receiver.close(); }
if (socket != null) {
socket.close();
}

Copilot uses AI. Check for mistakes.
final String serverKey = createServerKey(serverURL, port);

if (scope.getAgent() != null) {
scope.getAgent().getPopulation().addListener(new NetworkListener());
Copy link

Copilot AI Jun 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registering a new listener on every connect causes multiple redundant listeners; ensure only one listener per population by tracking registration or moving it to initialization.

Suggested change
scope.getAgent().getPopulation().addListener(new NetworkListener());
IPopulation population = scope.getAgent().getPopulation();
if (!registeredPopulations.contains(population)) {
population.addListener(new NetworkListener());
registeredPopulations.add(population);
}

Copilot uses AI. Check for mistakes.
final Map<String, IConnector> connectors = this.getRegisteredServers(scope);
final Map<String, IConnector> connectors = getRegisteredServers(scope);
final IAgent agent = scope.getAgent();
List<String> serverList = (List<String>) agent.getAttribute(INetworkSkill.NET_AGENT_SERVER);
Copy link

Copilot AI Jun 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Directly casting and using serverList without checking for null can cause a NullPointerException if the attribute is missing; consider initializing or validating it first.

Suggested change
List<String> serverList = (List<String>) agent.getAttribute(INetworkSkill.NET_AGENT_SERVER);
List<String> serverList = (List<String>) agent.getAttribute(INetworkSkill.NET_AGENT_SERVER);
if (serverList == null) {
serverList = new ArrayList<>();
}

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

[network] can't close simulation while connection is open

3 participants